package com.projectName.www.servlet;

import com.projectName.www.po.User;
import com.projectName.www.service.UserService;
import com.projectName.www.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;

    @WebServlet("/addMoneyServlet")
    public class AddMoneyServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            //1.设置编码
            request.setCharacterEncoding("utf-8");
            //2.获取参数

            String moneynum = request.getParameter("moneynum");
            HttpSession session=request.getSession();
            User user = (User)session.getAttribute("user");

            System.out.println("tiaoshi----------------------");
            System.out.println(user);
            System.out.println(moneynum);


            //3.封装对象


            //4.调用Service保存
            UserService service = new UserServiceImpl();
            if(user!=null&&moneynum!=null){

                service.addMoney(user.getuId().toString(),new BigDecimal(moneynum));
                User newuser = service.findByID(user.getuId().toString());
                session.setAttribute("user",newuser);
            }
            //5.跳转到userListServlet

            System.out.println(user+"------------------------------------addmoneySERvlt");
            request.getRequestDispatcher("/addmoney.jsp").forward(request,response);
//            response.sendRedirect(request.getContextPath()+"/dishListServlet");
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }
